<html>
<head>
<script>
chrome.browserAction.onClicked.addListener(
	function(tab)
	{
	});

var BOOKMARKS_XML = 'https://www.google.com/bookmarks/?output=xml&num=100000&sort=';
var NO_LABEL = 'no_label';
var STATUS_READY = 0;
var STATUS_LOADING = 1;
var STATUS_LOGIN = 2;
var status = STATUS_READY;
var bookmarks_count;
var bmk_count;

function init()
{
}

function getStatus()
{
	return status;
}

function startImport()
{
	if (status != STATUS_LOADING)
	{
		status = STATUS_LOADING;
		sendRequest();
	}
}

function sendRequest()
{
	var http = new XMLHttpRequest();
	http.open('GET', BOOKMARKS_XML, true);
	http.onreadystatechange = function()
	{
		if (http.readyState != 4 /*STATE_DONE*/) return;
		if (http.responseXML)
		{
			console.log('Successful response. Building bookmarks...');
			buildBookmarks(buildTree(http.responseXML));
		}
		else
		{
			console.log('Null response. Authorization is required...');
			status = STATUS_LOGIN;
		}
	};
	http.send();
}

function buildTree(xmlDoc)
{
	var tree = new Object();

	// build labels
	var xmlAllLabels = xmlDoc.getElementsByTagName('label');
	for (var i = 0; i < xmlAllLabels.length; i++)
	{
		var label = xmlAllLabels[i].firstChild.nodeValue;
		if (tree[label] == undefined)
		{
			tree[label] = new Array();
		}
	}
	tree[NO_LABEL] = new Array();

	// build bookmarks
	bookmarks_count = 0
	var xmlBookmarks = xmlDoc.getElementsByTagName('bookmark');
	for (var i = 0; i < xmlBookmarks.length; i++)
	{
		var bookmark = new Object();
		bookmark.url = xmlBookmarks[i].getElementsByTagName('url')[0].firstChild.nodeValue;
		var xmlTitles = xmlBookmarks[i].getElementsByTagName('title');
		if (xmlTitles.length > 0)
			bookmark.title = xmlTitles[0].firstChild.nodeValue;
		else
			bookmark.title = bookmark.url;
		var xmlBmkLabels = xmlBookmarks[i].getElementsByTagName('label');
		if (xmlBmkLabels.length == 0)
		{
			tree[NO_LABEL].push(bookmark);
			++bookmarks_count;
		}
		for (var j = 0; j < xmlBmkLabels.length; j++)
		{
			tree[xmlBmkLabels[j].firstChild.nodeValue].push(bookmark);
			++bookmarks_count;
		}
	}

	console.log(bookmarks_count + ' bookmarks were successfully loaded')
	return tree;
}

function buildBookmarks(tree)
{
	var labelsArray = new Array();
	for (label in tree)
	{
		if (tree[label].length == 0)
			continue;
		labelsArray.push(label);
	}
	labelsArray.sort();

	chrome.bookmarks.create(
		{parentId: '1', title: 'gb_import'},
		function(root)
		{
			if (!root) return;
			console.log('Tree root \''+ root.title + '\' was createed');
			bmk_count = 0;
			for (var i = 0; i < labelsArray.length; i++)
			{
				chrome.bookmarks.create(
					{parentId: root.id, title: labelsArray[i]},
					function(label)
					{
						if (!label) return;
						for (var j = 0; j < tree[label.title].length; j++)
						{
							chrome.bookmarks.create(
								{
									parentId: label.id,
									title: tree[label.title][j].title,
									url: tree[label.title][j].url
								},
								function(bmk)
								{
									if (++bmk_count == bookmarks_count)
									{
										console.log(bmk_count + ' bookmarks were added  for ' + labelsArray.length + ' labels');
										status = STATUS_READY;
									}
								});
						}
					});
			}
		});
}
</script>
</head>
<body onload="init()"></body>
</html>